package com.xinruke.hostels.reservation.user.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.MhHuiyuan;
import com.xinruke.hostels.common.util.CustomSecurity;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.reservation.user.interfaces.dto.UserDetailQueryDto;
import com.xinruke.hostels.reservation.user.interfaces.dto.UserInfoQueryDTO;
import com.xinruke.hostels.reservation.user.interfaces.dto.UserInfoQueryResultDTO;
import com.xinruke.hostels.reservation.user.mapper.MhHuiyuanMapper;
import com.xinruke.hostels.reservation.user.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserInfoServiceImpl
 * @CreateDate 2019/6/9
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 用户信息Service类
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    MhHuiyuanMapper huiyuanMapper;

    /**
     * 查询用户信息集合
     *
     * @param userInfoQueryDTO
     * @return
     */
    @Override
    public RowsDataVO<UserInfoQueryResultDTO> getUserInfoList(UserInfoQueryDTO userInfoQueryDTO) {
        RowsDataVO<UserInfoQueryResultDTO> rowsDataVO = new RowsDataVO<>();
        List<UserInfoQueryResultDTO> resultList = new ArrayList<>();

        MyExample example = new MyExample(MhHuiyuan.class, userInfoQueryDTO.getQueryConditions(), userInfoQueryDTO.getOrderFields());

        Page<MhHuiyuan> page = PageHelper.startPage(userInfoQueryDTO.getPageNo(), userInfoQueryDTO.getPageSize());
        List<MhHuiyuan> list = huiyuanMapper.selectByExample(example);

        if (list != null && list.size() > 0) {
            for (MhHuiyuan huiyuan : list) {
                UserInfoQueryResultDTO userInfoQueryResultDTO = new UserInfoQueryResultDTO();
                BeanUtils.copyProperties(huiyuan, userInfoQueryResultDTO);
                userInfoQueryResultDTO.setShenfenzhenghao(CustomSecurity.hideIDCard(userInfoQueryResultDTO.getShenfenzhenghao()));
                userInfoQueryResultDTO.setShoujihao(CustomSecurity.hideMobile(userInfoQueryResultDTO.getShoujihao()));
                resultList.add(userInfoQueryResultDTO);
            }
        }

        rowsDataVO.setPageNo(page.getPageNum());
        rowsDataVO.setTotalRecords(page.getTotal());
        rowsDataVO.setTotalPages(page.getPages());
        rowsDataVO.setRows(resultList);

        return rowsDataVO;
    }

    /**
     * 通过用户id得到用户信息
     * @param userInfoQueryDTO
     * @return
     */
    @Override
    public UserInfoQueryResultDTO getUserDetail(UserDetailQueryDto userInfoQueryDTO) {
        UserInfoQueryResultDTO resultDTO = new UserInfoQueryResultDTO();
        MhHuiyuan mhHuiyuan = huiyuanMapper.selectByPrimaryKey(userInfoQueryDTO.getId());
        BeanUtils.copyProperties(mhHuiyuan,resultDTO);
        return resultDTO;
    }

}
